<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2018/11/29
 * Time: 11:11
 */
namespace app\api\controller;
use think\Db;
class Info extends Base
{
    /**
     * 异地登录验证
     * @return string|void
     */
    public function _initialize()
    {
        extract(input());
        $auth = $this->checkAll($user_id,$time,$value,$reqKey);
        if($auth == false){
            return json_encode(['status'=>301,'msg'=>"Token失效！请重新登录！"]);
        }
    }

    /**
     * 获取古建筑资料列表
     * $page 页码
     * @return \think\response\Json
     */
    public function getGuInfo(){
        extract(input());
        $data = Db::name('build_info')
            ->field(['build_id',"if(build_thumb!='',concat('".http_type ()."',build_thumb),build_thumb)"=>'thumb','build_name','build_abstract','build_create_time'])
            ->page($page,10)
            ->order('build_create_time desc')
            ->select();
        foreach ($data as $key=>$vo){
            $data[$key]['build_create_time'] = date('Y-m-d H:i',strtotime ($vo['build_create_time']));
        }
        return json(['status'=>200,'msg'=>$data]);
    }

    /**
     * 获取古建筑详情
     * $build_id 古建id
     * @return \think\response\Json
     */
    public function getGuDetails(){
        extract(input());
        $data = Db::name('build_info')
            ->where(['build_id'=>$build_id])
            ->field(['build_id','build_tag','build_art','build_type',"if(build_music!='',concat('".http_type ()."',build_music),build_music)"=>'music','build_music_name','build_name','build_intro','build_zip'])
            ->find();
        return json(['status'=>200,'msg'=>$data]);
    }

    /**
     * 搜索古建筑
     * $key 关键字
     * $page 页码
     * @return \think\response\Json
     */
    public function searchGu(){
        extract(input());
        $data = Db::name('build_info')
            ->where(['build_name'=>['like',"%" . $key . "%"]])
            ->field(['build_id',"if(build_thumb!='',concat('".http_type ()."',build_thumb),build_thumb)"=>'thumb','build_name','build_abstract'])
            ->page($page,10)
            ->order('build_create_time desc')
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }

    /**
     * 今日入库/今日审核
     * $page 页码
     * @return \think\response\Json
     */
    public function todayIn(){
        extract(input());
        $role = Db::name('u_user')
            ->where(['user_id'=>$user_id])
            ->value('user_role');
        $map['in_status'] = 2;
        if($role == 1){//入库员
            $map['in_user_id'] = $user_id;
        }else{//审核员
            $map['check_user_id'] = $user_id;
        }
        $data = Db::name('in_warehouse w')
            ->join('u_user u','u.user_id = w.in_user_id','left')
            ->join('warehouse h','w.warehouse_id = h.house_id','left')
            ->where($map)
            ->field(['in_id','user_username',"if(user_image!='',concat('".http_type ()."',user_image),user_image)"=>'image','in_name','in_num','warehouse_name'])
            ->page($page,10)
            ->order('check_time desc')
            ->select();
        return json(['status'=>200,'msg'=>$data]);
    }

    /**
     * 今日入库/今日审核详情
     * $in_id 入库物件id
     * @return \think\response\Json
     */
    public function todayInDetails(){
        extract(input());
        $data = Db::name('in_warehouse w')
            ->join('u_user u','u.user_id = w.in_user_id','left')
            ->join('warehouse h','w.warehouse_id = h.house_id','left')
            ->join('house_type t','w.house_type_id = t.house_type_id','left')
            ->join('spec_type s','w.spec_type_id = s.spec_type_id','left')
            ->where(['in_id'=>$in_id])
            ->field(['in_id','user_username',"if(user_image!='',concat('".http_type ()."',user_image),user_image)"=>'image','in_name','in_num','warehouse_name','house_type','spec_type','in_address','in_abstract'])
            ->find();
        return json(['status'=>200,'msg'=>$data]);
    }
}